<template>
  <j-modal
    switchFullscreen
    :title="title"
    :width="500"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @ok="handleOk"
    @cancel="handleCancel"
    cancelText="关闭"
    wrapClassName="ant-modal-cust-warp"
    style="top:5%;height: 85%;overflow-y: hidden">
    <template slot="footer">
      <a-button type="primary" @click="handleCancel">取消</a-button>
      <a-button type="primary" @click="handleOk" v-if="!disableSubmit">确认</a-button>
    </template>
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="业务规则">
          <cn-dict-select-tag  v-decorator="['businessKey', validatorRules.businessKey]" placeholder="请选择业务规则"
                              :triggerChange="true" dictCode="rule_key" :disabled="disableSubmit"/>
        </a-form-item>

        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始字符">
          <a-input placeholder="请输入开始字符"  v-decorator.trim="[ 'startStr',{}]" :disabled="disableSubmit" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="时间格式">
          <cn-dict-select-tag  v-decorator="['timeStr', {}]" placeholder="请选择时间格式"
                              :triggerChange="true" dictCode="time_formatter" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="结尾字符">
          <a-input placeholder="请输入结尾字符"  v-decorator.trim="[ 'endStr',{}]" :disabled="disableSubmit" />
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="变动字符长度">
          <a-input-number placeholder="请输入变动字符长度"  v-decorator.trim="[ 'codeLength',
          {...validatorRules.codeLength,initialValue:1}]" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="递增数">
          <a-input-number placeholder="请输入递增数"  v-decorator.trim="[ 'increNum',{...validatorRules.increNum,initialValue:1}]" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="开始值">
          <a-input-number placeholder="请输入开始值"  v-decorator.trim="[ 'startVal',{initialValue:1}]" :disabled="disableSubmit"/>
        </a-form-item>
        <a-form-item
          :labelCol="labelCol"
          :wrapperCol="wrapperCol"
          label="当前值">
          <a-input-number placeholder="请输入当前值"  v-decorator.trim="[ 'nowVal',{initialValue:1}]" :disabled="disableSubmit"/>
        </a-form-item>
      </a-form>
    </a-spin>
  </j-modal>
</template>

<script>
  import {pick} from 'lodash'
  import {addRole,editRole,duplicateCheck } from '@/api/api'
  import {httpAction} from '@/api/manage'
  export default {
    name: "RuleCodeModal",
    data () {
      return {
        title:"操作",
        visible: false,
        roleDisabled: false,
        model: {},
        labelCol: {
          xs: { span: 24 },
          sm: { span: 6 },
        },
        wrapperCol: {
          xs: { span: 24 },
          sm: { span: 16 },
        },
        confirmLoading: false,
        form: this.$form.createForm(this),
        validatorRules:{
          businessKey:{
            rules: [
              { required: true, message: '' }
            ]},
          increNum:{
            rules: [
              { required: true, message: '' }
            ]},

          codeLength:{
            rules: [
              { required: true, message: '' }
            ]}
        },
        url: {
          add:window._CONFIG['domianURL']+ "/cnCodeRuleController/saveOrUpdate",
        },
      }
    },
    created () {
    },
    methods: {
      add () {
        this.edit({});
      },
      edit (record) {
        this.form.resetFields();
        this.model = Object.assign({}, record);
        this.visible = true;

        this.$nextTick(() => {
          this.form.setFieldsValue(pick(this.model,Object.keys(this.model)))
        });

      },
      close () {
        this.$emit('close');
        this.visible = false;
        this.confirmLoading = false;
      },
      handleOk () {
        const that = this;
        // 触发表单验证
        this.form.validateFields((err, values) => {
          if (!err) {
            that.confirmLoading = true;
            let httpurl = '';
            let method = '';
            httpurl+=this.url.add;
            method = 'post';
            let formData = Object.assign(this.model, values);
            //时间格式化
            console.log(formData)
            httpAction(httpurl,formData,method).then((res)=>{
              if(res.success){
                that.$message.success(res.message);
                that.$emit('ok');
                that.close();
              }else{
                that.$message.warning(res.message);
              }
            }).finally(() => {
              that.confirmLoading = false;
            })
          }
        })
      },
      handleCancel () {
        this.close()
      },
      validateRoleCode(rule, value, callback){
        if(/[\u4E00-\u9FA5]/g.test(value)){
          callback("");
        }else{
          var params = {
            tableName: "sys_role",
            fieldName: "role_code",
            fieldVal: value,
            dataId: this.model.id,
          };
          callback()
          // duplicateCheck(params).then((res)=>{
          //   if(res.success){
          //     callback();
          //   }else{
          //     callback(res.message);
          //   }
          // });
        }
      }

    }
  }
</script>

<style scoped>
.ant-input-number{
  width: 100%;
}
</style>
